package cn.silver.framework.web.controller;

import cn.silver.framework.core.utils.ServletUtils;
import cn.silver.framework.core.utils.StringUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.LocaleResolver;

import java.util.Locale;

/**
 * web层通用数据处理
 *
 * @author Silver Zhou
 */
@Slf4j
public abstract class ApiController implements LocaleResolver {

    /**
     * 页面跳转
     */
    public String redirect(String url) {
        return StringUtils.format("redirect:{}", url);
    }

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String language = ServletUtils.getHeader(request, "content-language");
        Locale locale = Locale.getDefault();
        if (StringUtils.isNotBlank(language)) {
            String[] split = language.split("_");
            locale = new Locale(split[0], split[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse httpServletResponse, Locale locale) {

    }
}
